//*****************************************************************
// PIDControlAlgorithm.h
//
//  Created on: Jan 21, 2012
//      Author: Christopher Wood
//              Robert Livosi
//
//  $Id: PIDControlAlgorithm.h 32 2012-01-30 04:49:23Z caw4567@gmail.com $
//*****************************************************************

#ifndef PIDCONTROLALGORITHM_H_
#define PIDCONTROLALGORITHM_H_

#include "ControlAlgorithm.h"

class PIDControlAlgorithm : public ControlAlgorithm
{
public:
	/**
	 * Default constructor.
	 */
	PIDControlAlgorithm();

	/**
	 * Default destructor.
	 */
	virtual ~PIDControlAlgorithm();

	/**
	 * Method that is used to prompt the user for the
	 * new algorithm parameters.
	 *
	 * @return a string for the user to interpret.
	 */
	virtual string getParamString();

	/**
	 * Helper method to determine the number of parameters
	 *
	 * @return the number of parameters needed to collect
	 */
	virtual int getParamLength();

	/**
	 * Set the parameters of the algorithm.
	 *
	 * @param params - string of new parameters
	 * @returns true if parsing was successful, false otherwise.
	 */
	virtual bool setParams(vector<string> params);

	/**
	 * Compute the transform based on the specified input.
	 *
	 * @param reference - the desired set point
	 * @param input - the new input
	 * @return the new output
	 */
	virtual float computeTransform(float reference, float input);

	/**
	 * Reset the control algorithm history buffers.
	 */
	virtual void reset();

private:
	// Fixed size for history buffers (based on transform function)
	static const uint8_t OUTPUT_SIZE = 3;

	// The number of parameters
	static const uint8_t NUM_PARAMS = 3;

	// PID parameters that are managed by the control thread (user input)
	volatile float kp;
	volatile float ki;
	volatile float kd;

	// History buffer indices
	int8_t uIndex;
	int8_t eIndex;

	// Buffers to hold signal history
	float uBuffer[OUTPUT_SIZE];
	float eBuffer[OUTPUT_SIZE];

	// Update semaphore
	sem_t updateSem;
};

#endif /* PIDCONTROLALGORITHM_H_ */
